package cn.nexd.location.indoor.core.mathematician;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import cn.nexd.location.indoor.bean.IndoorLocationData;

/**
 * Created by codngfish on 16/5/24.
 */
public class IndoorLocation {

    private static String[] wifiList;
    private long currentFloorId;

    public IndoorLocation() {
    }

    public void initLocationConfig(String optparam, String wp, String wifilist) throws IOException {
        //        IndoorLocationEngine.freeEngine();
        //        IndoorLocationEngine.initEngine(wp, optparam);
        IndoorLocationEngine.freeEngine();
        IndoorLocationEngine.initEngine(wp, optparam);
        wifiList = loadWifiList(new FileInputStream(wifilist));
    }

    public void initDetectionConfig(String conf, String model, String wifiList) throws IOException {
        Detection.setDetectionConfig(conf, model, wifiList);
    }

    public void setCurrentBuildingId(long currentBuildingId) {
        //        IndoorLocationEngine.setCurrentBuildingId(currentBuildingId);
        Detection.setCurrentBuildingId(currentBuildingId);
    }

    public void free() {
        currentFloorId = -1L;
        //        IndoorLocationEngine.freeEngine();
        IndoorLocationEngine.freeEngine();
    }

    public long getCurrentBuildingId() {
        //        return IndoorLocationEngine.getCurrentBuildingId();
        return Detection.getCurrentBuildingId();
    }

    public void setCurrentFloorId(long currentFloorId) {
        //        IndoorLocationEngine.setCurrentFloorId(currentFloorId);
        this.currentFloorId = currentFloorId;
    }

    public long getCurrentFloorId() {
        return currentFloorId;
    }

    // 定位,但是不切层
    public LocationEngineResult locationIndoorPosition(List<IndoorLocationData> indoorLocationDatas, long buildingId, long floorId) {
        return formatLocationResult(buildingId, floorId, IndoorLocationEngine.locatePostion(NexdIndoorWifiExtractor.extractWifi(wifiList, indoorLocationDatas)));
        //        return formatLocationResult(buildingId, floorId, IndoorLocationEngine.locatePostion(NexdIndoorWifiExtractor.extractWifi(wifiList, indoorLocationDatas)));
    }

    // 定位,切层
    public long locationDetection(List<IndoorLocationData> indoorLocationDatas) {
        return Detection.getLocationFloorId(indoorLocationDatas);
    }

    private final static LocationEngineResult formatLocationResult(long buildingId, long floorId, double[] result) {
        return new LocationEngineResult((float) result[0], (float) result[1], buildingId, floorId);
    }

    private final static String[] loadWifiList(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String[] wifi_list = null;
        try {
            //            List<String> res = new ArrayList<>();
            String line = bufferedReader.readLine();
            wifi_list = line.split(",");
        } finally {
            bufferedReader.close();
        }
        return wifi_list;
    }
}
